from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


def readLog():
    newLogFile = open('target.txt', 'w', encoding="utf-8")  # 清空文件内容再写
    with open('111.txt', 'r') as f:
        list1 = f.readlines()
        for i in range(0, len(list1)):
            text = list1[i].rstrip('\n').strip()
            log = decrypt(text).rstrip('\n').strip()  # 解密
            print("log "+log)
            if log!=None:
              newLogFile.write(log)
              newLogFile.write('\n')
    newLogFile.close()

def add_to_16(text):
    if len(text.encode('utf-8')) % 16:
        add = 16 - (len(text.encode('utf-8')) % 16)
    else:
        add = 0
    text = text + ('\0' * add)
    return text.encode('utf-8')


def encrypt(text):
    key = 'f01510daddd18ef6'.encode('utf-8')
    mode = AES.MODE_ECB
    text = add_to_16(text)
    cryptos = AES.new(key, mode)

    cipher_text = cryptos.encrypt(text)
    return b2a_hex(cipher_text)

def supplement(text):
    # 如果text不是16的倍数【加密文本text必须为16的倍数！】，那就补足为16的倍数
    text = text.encode("utf-8")
    # 这里密钥key 长度必须为16（AES-128）、24（AES-192）、或32（AES-256）Bytes 长度.目前AES-128足够用
    length = 16
    count = len(text)
    add = length - (count % length)
    text = text + (b'\0' * add)
    return text

def decrypt(text):
    key = supplement('sdfoJBND47')
    mode = AES.MODE_ECB
    try:
        cryptor = AES.new(key, mode)
        plain_text = cryptor.decrypt(a2b_hex(text))
    except:
        return text;
    else:
        return bytes.decode(plain_text,'utf-8').rstrip('\0')



if __name__ == '__main__':
    readLog()
    # text = "78D376CC5F9AD41FBFCB1948F5BAABFD9BE9DE90CECF564EFB0443DF4303FA901190D98019B5AED9AD542D5EEDE3DC6B717B9C4EDB00A51198D16772A65B766E2FF201968E227D338D61A13A40C254A4A2A72108FF8E6F02FDDA950773363D83268310D6BBDD772FB084CB386F2203A21DBBEA9E0E4BACC5B7BEFAE10B1F16D3"
    # a = decrypt(text)
    # print(a)

